package com.ophiux.wechat.servlet;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ophiux.wechat.db.MchAppModel;
import com.ophiux.wechat.db.Mch_App_Map;
import com.ophiux.wechat.utils.StringUtil;

/**
 * 授权地址入口(九院使用，使用OauthServlet）
 * @author chenh
 * wechatpay.ophiux.com/OphiuxWeChatPayPlatformbuild/RedirectServlet?appid=wx4a81b91ffb2bb6ed
 * &appsecret=D310d5f9a57bbb8ff3ebd9597beacd554&client_back_url=null

wechatpay.ophiux.com/OphiuxWeChatPayPlatformbuild/OauthServlet?appid=wx4a81b91ffb2bb6ed&appsecret=D310d5
f9a57bbb8ff3ebd9597beacd554&client_back_url=null
 *http://wechatpay.ophiux.com/OphiuxWeChatPayPlatformbuild/RedirectServlet?orderno=&money=&submchid=&
 */
public class OauthServlet extends HttpServlet {
	private static final long serialVersionUID = 8109906492690077077L;
	private static Logger logger = LoggerFactory.getLogger(OauthServlet.class);

//	private String REDIRECT_URL ="http://wechatpay.ophiux.com/OphiuxWeChatOauth/RedirectServlet";
	private String REDIRECT_URL ="http://hhl.easy.echosite.cn/OphiuxWeChatPayPlatformbuild/RedirectServlet";
//	private String REDIRECT_URL ="http://wechatpay.ophiux.com/OphiuxWeChatPayPlatformbuild/RedirectServlet";
//	private String REDIRECT_URL ="http://wx.hchongren.com/OphiuxWeChatPayPlatformbuild/RedirectServlet";
//	private String REDIRECT_URL = "http://app.cqszyy.com/OphiuxWeChatPayPlatformbuild/RedirectServlet";

	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 2017-11-30 sqx 
		 * 取消前端 明文传递appid，appsecret 
		 * 使用token 获取商户信息
		 */
//		String appid = request.getParameter("appid");
//		String appsecret = request.getParameter("appsecret");
		String token = request.getParameter("token");
		MchAppModel mchApp = Mch_App_Map.getMchAppModelByMchId(token);
		
		String appid="";
		String appsecret ="";
		if(!StringUtil.isEmpty(token)) {
			appid = mchApp.getAppid();
			appsecret = mchApp.getAppsecret();
		}else {
			appid = request.getParameter("appid");
			appsecret = request.getParameter("appsecret");
		}
		
		/**
		 * 回调至终端页面
		 */
		String client_back_url = request.getParameter("client_back_url");
		logger.info("回调至终端页面地址client_back_url="+client_back_url);
		client_back_url = URLEncoder.encode(client_back_url,"utf-8");
		
		String backUri = REDIRECT_URL;
		
 		backUri = backUri+"?appid="+appid+"&appsecret="+appsecret+"&client_back_url="+client_back_url+"&token="+token;
//		backUri = backUri+"?client_back_url="+client_back_url+"&token="+token;
 		logger.info("REDIRECT_URL(回调地址)加密之前:"+backUri);
 	 	backUri = URLEncoder.encode(backUri,"utf-8");
		logger.info("REDIRECT_URL(回调地址)加密之后:"+backUri);
 		
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
				"appid=" + appid+
				"&redirect_uri=" +
				 backUri+
				"&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
		//调用第三方地址
//		url = WeChatConfig.OAUTH_URL + "?RedirectUri="+backUri;
		logger.info("接收到终端授权请求，授权链接为："+url);
		//重定向
		response.sendRedirect(url);
		
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
